package com.qh.yyxt.service.Impl;

import com.qh.yyxt.pojo.entity.meeting.User;
import com.qh.yyxt.mapper.UserMapper;
import com.qh.yyxt.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class UserServiceImpl implements UserService
{
    @Autowired
    private UserMapper userMapper;


//    @Override
//    public User regist(String username, String password) {
//        return userMapper.findUser(username,password);;
//    }


    @Override
    public int register(String username,String password,String salt,String tel,String role)
    {
        if(userMapper.findUserByUsername(username)==null)
        {
            User user=new User();
            user.setPassword(password);
            user.setRole(role);
            user.setSalt(salt);
            user.setUsername(username);
            return userMapper.add(user);
        }
        return 0;
    }


    @Override
    public User findByUsername(String username) {
        return userMapper.findUserByUsername(username);
    }

    @Override
    //    * 修改用户密码
    //	 * @param password 输入的没加密的旧密码
    //	 * @param newPwd 输入的没加密的新密码
    //	 * @param cfgPwd 确认密码
    public String updatePassword(String password, String newPwd, String cfgPwd)
    {
        if(password==null || password.length()==0)
        {
            return "请输入旧密码！";
        }
        if(newPwd==null || newPwd.length()==0) {
            return  "请输入新密码！";
        }
        if(cfgPwd==null || cfgPwd.length()==0) {
            return "请确认新密码！";
        }
        //2.1判断两次输入的新密码是否一致
        if(!newPwd.equals(cfgPwd)) {
            return "两次输入密码不一致！";
        }
        //2.2判断旧密码是否跟登录密码一致
        //2.2.1 先获取用户的登陆身份通过shiro认证管理工具获得
        User user=(User)SecurityUtils.getSubject().getPrincipal();
        SimpleHash sh = new SimpleHash("MD5", password, user.getSalt(),1024);
        if(!user.getPassword().equals(sh.toHex())) {
            return "输入的旧密码不正确！";
        }
        //2.3判断新旧密码是否一致
        if(password.equals(newPwd)) {
            return "新密码不能与旧密码相同！";
        }
        //3.更新密码
        //  System.out.println(newPwd);
        String salt = UUID.randomUUID().toString();
        sh = new SimpleHash("MD5", newPwd, salt,1024);
        //System.out.println(sh);
        int rows = userMapper.updatePwd(user.getUsername(), sh.toHex(), salt);
        // System.out.println(rows);
        if(rows==1)
        {
            return "true";
        }else{
            return "修改失败";
        }
    }
    @Override
    public String updateInfo(String tel1, String tel2, String tel3)
    {
        User user=(User)SecurityUtils.getSubject().getPrincipal();
        if(!tel1.equals(user.getTel1()))
        {
            tel1=user.getTel1();
        }
        if(!tel2.equals(user.getTel2()))
        {
            tel2=user.getTel2();
        }
        if(!tel3.equals(user.getTel3()))
        {
            tel3=user.getTel3();
        }

        if(userMapper.updateByUserName(user.getUsername(),tel1,  tel2,  tel3)==1)
        {
            return "true";
        }else {
            return "修改失败";
        }
    }

    @Override
    public String judgeOldPwd(String oldPwd)
    {

        User user=(User)SecurityUtils.getSubject().getPrincipal();
        SimpleHash sh = new SimpleHash("MD5", oldPwd, user.getSalt(),1024);
        if(!user.getPassword().equals(sh.toHex()))
        {
            return "输入的旧密码不正确！";
        }
        return "true";
    }

    @Override
    public int add(String username, String password, String salt,String name)
    {
        User user=new User();
        user.setPassword(password);
        user.setSalt(salt);
        user.setUsername(username);
        return userMapper.add(user);
    }
}
